package com.vitaf.cash.app.ui.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.vitaf.cash.app.R
import com.vitaf.cash.app.common.VitafComDlog
import com.vitaf.cash.app.databinding.VitafDlogPhotoBinding
import com.vitaf.cash.app.event.VitafEventPointType

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/22 17:59
 * @Annotation：拍照、相册选择 dialog
 */
class VitafPhotoDlog: VitafComDlog {
    private lateinit var binding: VitafDlogPhotoBinding
    private var mCallback: IDialogCallback? = null
    //默认选择拍照
    private var selecetedType = 1

    override val pageName: String = "Photo"

    override fun initializeVitafWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        pageRemark = remark
    }

    /**
     * 设置备注
     */
    fun setEventRemark(remark: String){
        pageRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun createVitafContentView(): View {
        binding = VitafDlogPhotoBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initializeVitafViews() {
        selecetedType = 1
        checkGombeCreditSelectType()
    }

    override fun setupVitafEventListeners() {
        setOnClickListener(binding.imgPhotoClose)//关闭
        setOnClickListener(binding.cameraLayout)//拍照
        setOnClickListener(binding.albumLayout)//相册选择
        setOnClickListener(binding.buttonConfirm)//确认
    }

    override fun loadVitafData() {

    }

    override fun onVitafEventViewClick(v: View?) {
        when(v?.id){
            R.id.cameraLayout -> {//拍照
                selecetedType = 1
                checkGombeCreditSelectType()
            }
            R.id.albumLayout -> {//相册选择
                selecetedType = 2
                checkGombeCreditSelectType()
            }
            R.id.buttonConfirm -> {//确定
                if (selecetedType == 1){
                    showPhotoCameraClick()
                } else {
                    showPhotoAlbumClick()
                }
                dismiss()
            }
            else -> {
                dismiss()
            }
        }
    }

    /**
     * 切换拍照、相册选择
     */
    private fun checkGombeCreditSelectType(){
        binding.cameraLayout.getShapeDrawableBuilder()
            .setSolidColor(getColor(R.color.gombe_credit_FFF9F9F7))
            .setStrokeColor(getColor(R.color.gombe_credit_FFF9F9F7))
            .intoBackground()
        binding.imgCamera.setImageResource(R.mipmap.gombe_credit_image_photo_camera_normal_icon)
        binding.textCamera.setTextColor(getColor(R.color.gombe_credit_ffa8a8a8))
        binding.imgCameraSelected.setImageResource(R.mipmap.gombe_credit_image_photo_normal_icon)

        binding.albumLayout.getShapeDrawableBuilder()
            .setSolidColor(getColor(R.color.gombe_credit_FFF9F9F7))
            .setStrokeColor(getColor(R.color.gombe_credit_FFF9F9F7))
            .intoBackground()
        binding.imgAlbum.setImageResource(R.mipmap.gombe_credit_image_photo_album_normal_icon)
        binding.textAlbum.setTextColor(getColor(R.color.gombe_credit_ffa8a8a8))
        binding.imgAlbumSelected.setImageResource(R.mipmap.gombe_credit_image_photo_normal_icon)

        when(selecetedType){
            1 -> {
                binding.cameraLayout.getShapeDrawableBuilder()
                    .setSolidColor(getColor(R.color.gombe_credit_fffff5b5))
                    .setStrokeColor(getColor(R.color.theme))
                    .intoBackground()
                binding.imgCamera.setImageResource(R.mipmap.gombe_credit_image_photo_camera_selected_icon)
                binding.textCamera.setTextColor(getColor(R.color.gombe_credit_ff1d1e20))
                binding.imgCameraSelected.setImageResource(R.mipmap.gombe_credit_image_photo_selected_icon)
            }
            else -> {
                binding.albumLayout.getShapeDrawableBuilder()
                    .setSolidColor(getColor(R.color.gombe_credit_fffff5b5))
                    .setStrokeColor(getColor(R.color.theme))
                    .intoBackground()
                binding.imgAlbum.setImageResource(R.mipmap.gombe_credit_image_photo_album_selected_icon)
                binding.textAlbum.setTextColor(getColor(R.color.gombe_credit_ff1d1e20))
                binding.imgAlbumSelected.setImageResource(R.mipmap.gombe_credit_image_photo_selected_icon)
            }
        }
    }

    /**
     * 拍照
     */
    private fun showPhotoCameraClick(){
        mCallback?.openCamera()
    }

    /**
     * 相册
     */
    private fun showPhotoAlbumClick(){
        mCallback?.openAlbum()
    }

    interface IDialogCallback {
        //打开相机
        fun openCamera()
        //打开相册
        fun openAlbum()
    }
}